package com.hzhr.iot.device.controller.base;

import com.hzhr.iot.core.constant.HzhrHeaderNames;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.device.Global;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@Component
public class BaseController {
    @Autowired
    HttpServletRequest request;
    @Autowired
    Global global;

    protected Map<String, String> initHeader() {
        Map<String, String> header = new HashMap<>();
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            header.put(headerName, request.getHeader(headerName));
        }

        header.put(HzhrHeaderNames.USER_ID, HzhrTypeUtil.object2string(request.getAttribute(HzhrHeaderNames.USER_ID)));
        header.put(HzhrHeaderNames.USER_CODE, HzhrTypeUtil.object2string(request.getAttribute(HzhrHeaderNames.USER_CODE)));
        header.put(HzhrHeaderNames.USER_NAME, HzhrTypeUtil.object2string(request.getAttribute(HzhrHeaderNames.USER_NAME)));
        header.put(HzhrHeaderNames.COMPANY_ID, HzhrTypeUtil.object2string(request.getAttribute(HzhrHeaderNames.COMPANY_ID)));
        header.put(HzhrHeaderNames.DEPT_ID, HzhrTypeUtil.object2string(request.getAttribute(HzhrHeaderNames.DEPT_ID)));
        header.put(HzhrHeaderNames.TRANSACTION_ID, HzhrTypeUtil.object2string(request.getAttribute(HzhrHeaderNames.TRANSACTION_ID)));
        header.put(HzhrHeaderNames.TRANSACTION_TYPE, HzhrTypeUtil.object2string(request.getAttribute(HzhrHeaderNames.TRANSACTION_TYPE)));
        header.put(HzhrHeaderNames.USER_TYPE, HzhrTypeUtil.object2string(request.getAttribute(HzhrHeaderNames.USER_TYPE)));
        header.put(HzhrHeaderNames.ACCESS_MODE, HzhrTypeUtil.object2string(request.getAttribute(HzhrHeaderNames.ACCESS_MODE)));

        return header;
    }
}
